package com.example.arithmeticleetcode.leetcode.jan;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-12 09:53
 **/
public class Demo09 {

    public int[] decode(int[] encoded, int first) {
        int[] retArr = new int[encoded.length + 1];
        retArr[0] = first;
        if (encoded.length == 0) {
            return retArr;
        }
        for (int i = 1; i <= encoded.length; i++) {
            int preIndex = i - 1;
            retArr[i] = encoded[preIndex] - retArr[preIndex];
        }
        return retArr;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode nextListNode = new ListNode(2);
        listNode.next = nextListNode;
//        swapNodes(listNode, 1);
        reversListNode(listNode);
    }

    /**
     * 1721. 交换链表中的节点
     *
     * @param head
     * @param k
     * @return
     */
    public static ListNode swapNode2(ListNode head, int k) {
        ListNode quickPoint = head;
        ListNode slowPoint = head;
        ListNode indexK = head;
        int n = 1;
        while (quickPoint.next != null) {
            if (n < k) {
                indexK = indexK.next;
            } else {
                slowPoint = slowPoint.next;
            }
            quickPoint = quickPoint.next;
            n++;
        }
        int temp = indexK.val;
        indexK.val = slowPoint.val;
        slowPoint.val = temp;
        return head;
    }

    private static ListNode reversListNode(ListNode listNode) {
        ListNode head = null;
        ListNode virtualListNode = listNode;
        while (virtualListNode != null) {
            ListNode temp = virtualListNode.next;
            virtualListNode.next = head;
            head = virtualListNode;
            virtualListNode = temp;
        }
        return head;
    }

    /**
     * 1721. 交换链表中的节点
     *
     * @param head
     * @param k
     * @return
     */
    public static ListNode swapNodes(ListNode head, int k) {
        ListNode virtualHead = new ListNode(0);
        ListNode tempVirtualHead = virtualHead;
        List<ListNode> list = new ArrayList<>();
        ListNode tempNode = head;
        while (tempNode != null) {
            list.add(tempNode);
            tempNode = tempNode.next;
        }
        if (list.size() == 0 || list.size() == 1) return head;
        ListNode headKNode = list.get(k - 1);
        list.set(k - 1, list.get(list.size() - k));
        list.set(list.size() - k, headKNode);
        for (ListNode listNode : list) {
            tempVirtualHead.next = listNode;
            tempVirtualHead = listNode;
        }
        tempVirtualHead.next = null;
        return virtualHead.next;
    }

}

 class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }

     @Override
     public String toString() {
         return "ListNode{" +
                 "val=" + val +
                 '}';
     }
 }